<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${package.Mapper}.${table.mapperName}">

<#if enableCache>
    <!-- 开启二级缓存 -->
    <cache type="org.mybatis.caches.ehcache.LoggingEhcache"/>

</#if>
<#if baseResultMap>
    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="${package.Entity}.${entity}">
<#list table.fields as field>
<#if field.keyFlag><#--生成主键排在第一位-->
        <id column="${field.name}" property="${field.propertyName}" />
</#if>
</#list>
<#list table.commonFields as field><#--生成公共字段 -->
    <result column="${field.name}" property="${field.propertyName}" />
</#list>
<#list table.fields as field>
<#if !field.keyFlag><#--生成普通字段 -->
        <result column="${field.name}" property="${field.propertyName}" />
</#if>
</#list>
    </resultMap>

</#if>
<#if baseColumnList>
    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
<#list table.commonFields as field>
        ${field.name},
</#list>
        <#--${table.fieldNames}-->
        <#list table.fields as field>`${field.name}`<#if field_has_next>, </#if></#list>
    </sql>

</#if>
<#if cfg.pfyPlugin_selective>
    <!--根据选择的属性进行插入-->
    <insert id="insertSelective" parameterType="map" <#list table.fields as field><#if field.keyFlag>keyColumn="${field.name}" keyProperty="${field.propertyName}" useGeneratedKeys="true"</#if></#list>>
        insert into ${table.name}
            <foreach close=")" collection="selective" item="column" open="(" separator=",">
                <#noparse>${</#noparse>column.column<#noparse>}</#noparse>
            </foreach>
        values
            <foreach close=")" collection="selective" item="column" open="(" separator=",">
                <#noparse>#{record.${column.javaProperty},javaType=${column.javaType}}</#noparse>
            </foreach>
    </insert>

    <!--根据选择的属性对主键对应的数据进行修改-->
    <update id="updateByPrimaryKeySelective" parameterType="map">
        update ${table.name}
        SET
            <foreach collection="selective" item="column" separator=",">
                <#noparse>${</#noparse>column.column<#noparse>}</#noparse> = <#noparse>#{record.${column.javaProperty},javaType=${column.javaType}}</#noparse>
            </foreach>
        where <#list table.fields as field><#if field.keyFlag>`${field.name}` =  <#noparse>#{</#noparse>record.${field.propertyName},javaType=${field.columnType}<#noparse>}</#noparse></#if></#list>
    </update>

</#if>
<#if cfg.pfyPlugin_batch>
    <!--批量插入记录-->
    <insert id="batchInsert" parameterType="map" <#list table.fields as field><#if field.keyFlag>keyColumn="${field.name}" keyProperty="${field.propertyName}" useGeneratedKeys="true"</#if></#list>>
        insert into ${table.name}
            ( <#list table.fields as field><#if !field.keyFlag>`${field.name}`<#if field_has_next>, </#if></#if></#list> )
        values
            <foreach collection="list" item="item" separator=",">
                ( <#list table.fields as field><#if !field.keyFlag><#noparse>#{</#noparse>item.${field.propertyName}<#noparse>}</#noparse><#if field_has_next>, </#if></#if></#list> )
            </foreach>
    </insert>

    <!--批量修改-->
    <insert id="batchUpsert" parameterType="map">
        insert into ${table.name}
            ( <#list table.fields as field>`${field.name}`<#if field_has_next>, </#if></#list> )
        values
            <foreach collection="list" item="item" separator=",">
                ( <#list table.fields as field><#noparse>#{</#noparse>item.${field.propertyName}<#noparse>}</#noparse><#if field_has_next>, </#if></#list> )
            </foreach>
        on duplicate key update
            <#list table.fields as field>`${field.name}` = values(`${field.name}`)<#if field_has_next>, </#if></#list>
    </insert>

</#if>
</mapper>
